﻿/// <summary>
/// Summary description for utilities
/// </summary>
[System.Web.Services.WebService(Namespace = "http://tempuri.org/")]
[System.Web.Services.WebServiceBinding(ConformsTo = System.Web.Services.WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class utilities : System.Web.Services.WebService
{
    public utilities () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

	#region Misc
	/// Property RequestInfo: Get
	private System.String RequestInfo
	{
		get
		{
			// create builder
			System.Text.StringBuilder sbReq = new System.Text.StringBuilder();

			try
			{
				////////////////////////////////// ALL PARAMS
				System.Text.StringBuilder sbReqParams = new System.Text.StringBuilder();
				for (System.Int32 i = 0; i < this.Context.Request.Params.AllKeys.Length; ++i)
				{
					// get key/value
					System.String key = this.Context.Request.Params.AllKeys[i];
					System.String value = this.Context.Request.Params[key];

					// add
					sbReqParams.AppendFormat("{0}={1}",
						key,
						this.Context.Request.Params[key]);

					// add new line
					if (i < this.Context.Request.Params.AllKeys.Length - 1)
					{
						sbReqParams.AppendLine();
					}
				}

				sbReq.Append("----------------------------------------------------------------- Services.utilities::REQUESTPARAMS -----------------------------------------------------------------");
				if (EFS.Common.Verify.String(sbReqParams.ToString()))
				{
					sbReq.AppendFormat("{0}{1}",
						System.Environment.NewLine,
						sbReqParams.ToString());
				}
			}
			catch (System.Exception ex)
			{
				EFS.EventLog.Log.LogException(ex, null);
			}

			// ok
			return sbReq.ToString();
		}
	}

	/// Adds an attribute
	private static void AddXmlAttribute(
		System.Xml.XmlNode xmlNode,
		System.String name,
		System.String value)
	{
		// check
		EFS.Common.Verify.Reference(xmlNode, "xmlNode");
		EFS.Common.Verify.String(name, "name");

		// create attribute
		System.Xml.XmlAttribute xmlAttribute = xmlNode.OwnerDocument.CreateAttribute(name);
		xmlAttribute.Value = value;

		// add error code
		xmlNode.Attributes.SetNamedItem(xmlAttribute);
	}

	/// Adds an error code to an xml node
	private static void AddXmlNodeErrorCode(
		System.Xml.XmlNode xmlNode,
		System.Int32 errorCode,
		System.String errorDescription)
	{
		// check
		EFS.Common.Verify.Reference(xmlNode, "xmlNode");

		/////////////////////////// ERROR CODE /////////////////////////
		AddXmlAttribute(xmlNode, "errorCode", errorCode.ToString());

		/////////////////////////// ERROR DESCRIPTION /////////////////////////
		if (EFS.Common.Verify.String(errorDescription))
		{
			AddXmlAttribute(xmlNode, "errorDescription", errorDescription);
		}
	}

	/// Adds an error code to an xml node
	private static System.Xml.XmlNode AddXmlNode(
		System.Xml.XmlNode xmlNodeParent,
		System.String nodeName,
		System.String nodeInnerText)
	{
		// check
		EFS.Common.Verify.Reference(xmlNodeParent, "xmlNodeParent");
		EFS.Common.Verify.String(nodeName, "nodeName");

		// create new
		System.Xml.XmlElement xmlElem = xmlNodeParent.OwnerDocument.CreateElement(nodeName);
		xmlElem.InnerText = nodeInnerText;

		// add
		xmlNodeParent.AppendChild(xmlElem);

		// ok
		return xmlElem;
	}

	/// GetColumnSetAttributes
	private System.String[] GetTrimmedSplittedString(
		System.String values,
		System.Char[] separator)
	{
		// get cols
		System.String[] retValue = values.Split(separator, System.StringSplitOptions.RemoveEmptyEntries);

		// cicle all
		for (System.Int32 i = 0; i < retValue.Length; ++i)
		{
			retValue[i] = EFS.Common.Functions.String.GetLowerTrimmedString(retValue[i]);
		}

		// ok
		return retValue;
	}

	/// ToXmlDocument
	private static System.Xml.XmlDocument ToXmlDocument(
		System.Object data)
	{
		System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
		xmlDoc.LoadXml(EFS.Common.Serialization.Functions.SerializeToString(data));
		return xmlDoc;
	}

	/// ToXmlDocument
	private static System.Xml.XmlDocument ToXmlDocument(
		System.Xml.XmlDocument xDocument)
	{
		System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
		xmlDoc.LoadXml(xDocument.ToString());

		// ok
		return xmlDoc;
	}
	#endregion

	#region Domain
	//get a given skill
	[System.Web.Services.WebMethod]
	public System.Xml.XmlDocument SearchDomainUsers(
		System.String partialName)
	{
		// prepare params
		System.String partialNameTemp = EFS.Common.Functions.String.GetTrimmedString(partialName);

		try
		{
			// get
			EFS.AD.User[] users = global::Domain.Instance.Filter(null, true, partialNameTemp, false);

			// ok
			return ToXmlDocument(users);
		}
		catch (System.Exception ex)
		{
			// log
			EFS.EventLog.Log.LogException(ex, null);
			throw ex;
		}
	}
	#endregion

	#region Skills
	//get a given skill
	[System.Web.Services.WebMethod]
	public System.Xml.XmlDocument Skills_GetDetails(System.Int32 id)
	{
		try
		{
			EFS.RC.CC.Deployment.Entities.Skill skillMsg = EFS.RC.CC.Deployment.Tables.Tools.Skills.GetByID(id);
			return ToXmlDocument(skillMsg);
		}
		catch (System.Exception ex)
		{
			EFS.EventLog.Log.LogException(ex, null);
			throw ex;
		}
	}

	//get all skills
	[System.Web.Services.WebMethod]
	public System.Xml.XmlDocument Skills_GetAll()
	{
		try
		{
			EFS.RC.CC.Deployment.Entities.Skill[] skillMsg = EFS.RC.CC.Deployment.Tables.Tools.Skills.GetByAll();
			return ToXmlDocument(skillMsg);
		}
		catch (System.Exception ex)
		{
			EFS.EventLog.Log.LogException(ex, null);
			throw ex;
		}
	}

	//update skill
	[System.Web.Services.WebMethod]
	public void Skill_Update(System.Int32 id, System.String name)
	{
		try
		{
			EFS.RC.CC.Deployment.Tables.Tools.Skills.Update(id, name);
		}
		catch (System.Exception ex)
		{
			EFS.EventLog.Log.LogException(ex, null);
			throw ex;
		}
	}

	//create skill
	[System.Web.Services.WebMethod]
	public System.Int32? Skill_Create(System.String name)
	{
		try
		{
			return EFS.RC.CC.Deployment.Tables.Tools.Skills.Create(name);
		}
		catch (System.Exception ex)
		{
			EFS.EventLog.Log.LogException(ex, null);
			throw ex;
		}
	}

	//delete skill
	[System.Web.Services.WebMethod]
	public void Skill_Delete(System.Int32 id)
	{
		try
		{
			EFS.RC.CC.Deployment.Tables.Tools.Skills.Delete(id);
		}
		catch (System.Exception ex)
		{
			EFS.EventLog.Log.LogException(ex, null);
			throw ex;
		}
	}
	#endregion

	#region AgentSkills
	//get all skills
	[System.Web.Services.WebMethod]
	public System.Xml.XmlDocument AgentSkill_GetByAgentID(System.Int32 agentID)
	{
		try
		{
			EFS.RC.CC.Deployment.Entities.Operators.Skills.Skill[] skillMsg = EFS.RC.CC.Deployment.Tables.Tools.AgentSkills.GetByAgentID(agentID);
			return ToXmlDocument(skillMsg);
		}
		catch (System.Exception ex)
		{
			EFS.EventLog.Log.LogException(ex, null);
			throw ex;
		}
	}
	#endregion

	#region Offices

	//get offices
	[System.Web.Services.WebMethod]
	public System.Xml.XmlDocument OfficeEndPoints_GetAll()
	{
		try
		{
			EFS.RC.CC.Deployment.Entities.EndPoints.Office[] officesMsg = EFS.RC.CC.Deployment.Tables.Tools.OfficeEndPoints.GetAll();
			return ToXmlDocument(officesMsg);
		}
		catch (System.Exception ex)
		{
			EFS.EventLog.Log.LogException(ex, null);
			throw ex;
		}
	}

	//create office
	[System.Web.Services.WebMethod]
	public System.Int32? OfficeEndPoint_Create(System.String sip)
	{
		try
		{
			return EFS.RC.CC.Deployment.Tables.Tools.OfficeEndPoints.Create(sip);
		}
		catch (System.Exception ex)
		{
			EFS.EventLog.Log.LogException(ex, null);
			throw ex;
		}
	}

	//delete office
	[System.Web.Services.WebMethod]
	public void OfficeEndPoint_Delete(System.Int32 id)
	{
		try
		{
			EFS.RC.CC.Deployment.Tables.Tools.OfficeEndPoints.Delete(id);
		}
		catch (System.Exception ex)
		{
			EFS.EventLog.Log.LogException(ex, null);
			throw ex;
		}
	}

	//update office
	[System.Web.Services.WebMethod]
	public void OfficeEndPoint_Update(System.Int32 id, System.String sip)
	{
		try
		{
			EFS.RC.CC.Deployment.Tables.Tools.OfficeEndPoints.Update(id, sip);
		}
		catch (System.Exception ex)
		{
			EFS.EventLog.Log.LogException(ex, null);
			throw ex;
		}
	}

	#endregion

	#region Agents

	//get agents
	[System.Web.Services.WebMethod]
	public System.Xml.XmlDocument AgentAD_GetByOfficeEndpointID(System.Int32 officeEndPointID)
	{
		try
		{
			EFS.RC.CC.Deployment.Entities.Operators.Agent[] agentsMsg = EFS.RC.CC.Deployment.Tables.Tools.Agents.GetByOfficeEndPointID(officeEndPointID);

			// create list
			System.Collections.Generic.List<EFS.RC.CC.Deployment.Views.Operators.AgentADDetails> retValue =
				new System.Collections.Generic.List<EFS.RC.CC.Deployment.Views.Operators.AgentADDetails>();

			// cicle all
			foreach (EFS.RC.CC.Deployment.Entities.Operators.Agent sup in agentsMsg)
			{
				EFS.RC.CC.Deployment.Views.Operators.AgentADDetails supAD =
					new EFS.RC.CC.Deployment.Views.Operators.AgentADDetails();
				supAD.ID = sup.ID;
				supAD.OfficeEndPointID = sup.OfficeEndPointID;

				// get from AD
				EFS.AD.User user = global::Domain.Instance.GetByGuid(sup.DomainOid.Value);
				if (null == user)
				{
					continue;
				}

				// set
				supAD.FirstName = user.FirstName;
				supAD.LastName = user.LastName;
				supAD.DomainLogonName = user.LogonName;
				supAD.SIP = user.SIP;

				// add to list
				retValue.Add(supAD);
			}

			// ok
			return ToXmlDocument(retValue.ToArray());
		}
		catch (System.Exception ex)
		{
			EFS.EventLog.Log.LogException(ex, null);
			throw ex;
		}
	}

	//get agents
	[System.Web.Services.WebMethod]
	public System.Xml.XmlDocument AgentAD_GetByID(System.Int32 agentID)
	{
		try
		{
			EFS.RC.CC.Deployment.Entities.Operators.Agent agent = EFS.RC.CC.Deployment.Tables.Tools.Agents.GetByID(agentID);

			EFS.RC.CC.Deployment.Views.Operators.AgentADDetails agentAD =
				new EFS.RC.CC.Deployment.Views.Operators.AgentADDetails();

			agentAD.ID = agent.ID;
			agentAD.OfficeEndPointID = agent.OfficeEndPointID;

			// get from AD
			EFS.AD.User user = global::Domain.Instance.GetByGuid(agent.DomainOid.Value);
			if (null == user)
			{
				throw new System.Exception("Agent not found ");
			}

			// set
			agentAD.FirstName = user.FirstName;
			agentAD.LastName = user.LastName;
			agentAD.DomainLogonName = user.LogonName;
			agentAD.SIP = user.SIP;

			// ok
			return ToXmlDocument(agentAD);
		}
		catch (System.Exception ex)
		{
			EFS.EventLog.Log.LogException(ex, null);
			throw ex;
		}
	}

	//create agent
	[System.Web.Services.WebMethod]
	public System.Int32? Agent_Create(
		System.Int32 officeEndPointID,
		System.String sDomainOid,
		System.String skillsXML)
	{
		//convert skills type
		EFS.RC.CC.Deployment.Entities.Operators.Skills.Skill[] skills =
			null == skillsXML ? null : EFS.Common.Serialization.Functions.Deserialize<EFS.RC.CC.Deployment.Entities.Operators.Skills.Skill[]>(skillsXML);

		// create connection
		using (EFS.DB.Connection connection = EFS.RC.CC.Deployment.Connection.New1)
		{
			// Start a local transaction
			connection.BeginTransaction();

			// make guid
			System.Guid domainOid = new System.Guid(sDomainOid);

			// get from AD
			EFS.AD.User user = global::Domain.Instance.GetByGuid(domainOid);
			if (null == user)
			{
				throw new System.Exception(System.String.Format("Could not find Domain User {0}", sDomainOid));
			}

			// create agent
			System.Int32? agentID =
				EFS.RC.CC.Deployment.Tables.Tools.Agents.Create(
					officeEndPointID,
					user.LogonName,
					domainOid,
					connection);
			if (!agentID.HasValue)
			{
				throw new System.Exception("Failed to create agent");
			}

			// create skills
			if (EFS.Common.Verify.Array(skills))
			{
				// cicle all and create
				foreach (EFS.RC.CC.Deployment.Entities.Operators.Skills.Skill agentSkill in skills)
				{
					// create
					EFS.RC.CC.Deployment.Tables.Tools.AgentSkills.Create(
						agentID.Value,
						agentSkill.SkillID.Value,
						agentSkill.SkillValue.Value,
						connection);
				}
			}

			// commit
			connection.CommitTransaction();

			// ok
			return agentID;
		}
	}

	//create agent
	[System.Web.Services.WebMethod]
	public void Agent_Update(
		System.Int32 id,
		System.String skillsXML)
	{
		//convert skills type
		EFS.RC.CC.Deployment.Entities.Operators.Skills.Skill[] skills = EFS.Common.Serialization.Functions.Deserialize<EFS.RC.CC.Deployment.Entities.Operators.Skills.Skill[]>(skillsXML);

		// create connection
		using(EFS.DB.Connection connection = EFS.RC.CC.Deployment.Connection.New1)
		{
			// Start a local transaction
			connection.BeginTransaction();

			//reset skills
			EFS.RC.CC.Deployment.Tables.Tools.AgentSkills.DeleteByAgentID(id, connection);

			// create skills
			if (EFS.Common.Verify.Array(skills))
			{
				// cicle all and create
				foreach (EFS.RC.CC.Deployment.Entities.Operators.Skills.Skill agentSkill in skills)
				{
					// create
					EFS.RC.CC.Deployment.Tables.Tools.AgentSkills.Create(
						id,
						agentSkill.SkillID.Value,
						agentSkill.SkillValue.Value,
						connection);
				}
			}

			// commit
			connection.CommitTransaction();
		}
	}

	//delete agent
	[System.Web.Services.WebMethod]
	public void Agent_Delete(System.Int32 id)
	{
		// create connection
		using(EFS.DB.Connection connection = EFS.RC.CC.Deployment.Connection.New1)
		{
			// Start a local transaction
			connection.BeginTransaction();
				
			//remove AgentSkills
			EFS.RC.CC.Deployment.Tables.Tools.AgentSkills.DeleteByAgentID(id, connection);

			//remove Agent
			EFS.RC.CC.Deployment.Tables.Tools.Agents.Delete(id, connection);

			//everything ok
			connection.CommitTransaction();
		}

	}

	#endregion

	#region Supervisors
	//get supervisors
	[System.Web.Services.WebMethod]
	public System.Xml.XmlDocument SupervisorAD_GetByOfficeEndpointID(System.Int32 officeEndPointID)
	{
		try
		{
			EFS.RC.CC.Deployment.Entities.Operators.Supervisor[] supervisorsMsg = EFS.RC.CC.Deployment.Tables.Tools.Supervisors.GetByOfficeEndPointID(officeEndPointID);

			// create list
			System.Collections.Generic.List<EFS.RC.CC.Deployment.Views.Operators.SupervisorADDetails> retValue =
				new System.Collections.Generic.List<EFS.RC.CC.Deployment.Views.Operators.SupervisorADDetails>();

			// cicle all
			foreach (EFS.RC.CC.Deployment.Entities.Operators.Supervisor sup in supervisorsMsg)
			{
				EFS.RC.CC.Deployment.Views.Operators.SupervisorADDetails supAD =
					new EFS.RC.CC.Deployment.Views.Operators.SupervisorADDetails();
				supAD.ID = sup.ID;
				supAD.OfficeEndPointID = sup.OfficeEndPointID;

				// get from AD
				EFS.AD.User user = global::Domain.Instance.GetByGuid(sup.DomainOid.Value);
				if (null == user)
				{
					continue;
				}

				// set
				supAD.FirstName = user.FirstName;
				supAD.LastName = user.LastName;
				supAD.DomainLogonName = user.LogonName;
				supAD.SIP = user.SIP;

				// add to list
				retValue.Add(supAD);
			}

			// ok
			return ToXmlDocument(retValue.ToArray());
		}
		catch (System.Exception ex)
		{
			EFS.EventLog.Log.LogException(ex, null);
			throw ex;
		}
	}

	//create supervisor
	[System.Web.Services.WebMethod]
	public System.Int32? Supervisor_Create(System.Int32 officeEndPointID, System.String sDomainOid)
	{
		try
		{
			// make guid
			System.Guid domainOid = new System.Guid(sDomainOid);

			// get from AD
			EFS.AD.User user = global::Domain.Instance.GetByGuid(domainOid);
			if (null == user)
			{
				return null;
			}

			// create
			return EFS.RC.CC.Deployment.Tables.Tools.Supervisors.Create(officeEndPointID, domainOid, user.LogonName);
		}
		catch (System.Exception ex)
		{
			EFS.EventLog.Log.LogException(ex, null);
			throw ex;
		}
	}

	//delete supervisor
	[System.Web.Services.WebMethod]
	public void Supervisor_Delete(System.Int32 id, System.Int32 officeEndPointID)
	{
		try
		{
			EFS.RC.CC.Deployment.Tables.Tools.Supervisors.Delete(id);
		}
		catch (System.Exception ex)
		{
			EFS.EventLog.Log.LogException(ex, null);
			throw ex;
		}
	}
	#endregion

	#region CustomersEndPoint

	//get customers
	[System.Web.Services.WebMethod]
	public System.Xml.XmlDocument CustomerEndPoints_GetAllDetails()
	{
		try
		{
			EFS.RC.CC.Deployment.Views.EndPoints.CustomerDetails[] customersMsg = EFS.RC.CC.Deployment.Tables.Tools.CustomerEndPoints.GetAllDetails();
			return ToXmlDocument(customersMsg);
		}
		catch (System.Exception ex)
		{
			EFS.EventLog.Log.LogException(ex, null);
			throw ex;
		}
	}

	//get customers
	[System.Web.Services.WebMethod]
	public void CustomerEndPoint_Delete(System.Int32 id)
	{
			//create connection
		using (EFS.DB.Connection connection = EFS.RC.CC.Deployment.Connection.New1)
		{
			// entry transaction
			connection.BeginTransaction();

			// delete
			EFS.RC.CC.Deployment.Tables.Tools.AgentDistributions.DeleteByCustomerEndpointID(id, connection);
			EFS.RC.CC.Deployment.Tables.Tools.CustomerEndPoints.Delete(id);

			// exit transaction
			connection.CommitTransaction();
		}
	}

	//create customer
	[System.Web.Services.WebMethod]
	public System.Int32? CustomerEndPoint_Create(System.String tel, System.Int32 skillID, System.Int32 endpointID)
	{
		try
		{
			//create endpoint
			return EFS.RC.CC.Deployment.Tables.Tools.CustomerEndPoints.Create(tel, skillID, endpointID);
		}
		catch (System.Exception ex)
		{
			EFS.EventLog.Log.LogException(ex, null);
			throw ex;
		}
	}

	//create customer
	[System.Web.Services.WebMethod]
	public void CustomerEndPoint_Update(System.Int32 id, System.String tel, System.Int32 skillID, System.Int32 officeEndPointID)
	{
		try
		{
			//update endpoint
			EFS.RC.CC.Deployment.Tables.Tools.CustomerEndPoints.Update(id, tel, skillID, officeEndPointID);
		}

		catch (System.Exception ex)
		{
			EFS.EventLog.Log.LogException(ex, null);
			throw ex;
		}
	}
	#endregion

	#region Surveys

	#region Survey

	//get all surveys
	[System.Web.Services.WebMethod]
	public System.Xml.XmlDocument Survey_GetAll(System.Int32? template, System.Int32? type, System.Int32? reviewType)
	{
		try
		{
			EFS.RC.CC.CH.Entities.Surveys.SurveyInfo[] surveyMsg = EFS.RC.CC.CH.Tables.Surveys.GetAll(template, (EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eType?)type, (EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType?)reviewType);
			return ToXmlDocument(surveyMsg);
		}
		catch (System.Exception ex)
		{
			// log
			EFS.EventLog.Log.LogException(ex, null);

			// throw
			throw ex;
		}
	}

	//update survey
	[System.Web.Services.WebMethod]
	public void Survey_Update(System.Int32 id, System.Int32 type, System.String name, System.Int32 reviewType)
	{
		try
		{
			EFS.RC.CC.CH.Tables.Surveys.Update(id, (EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eType)type, name, (EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType)reviewType);
		}
		catch (System.Exception ex)
		{
			EFS.EventLog.Log.LogException(ex, null);
			throw ex;
		}
	}

	//create survey
	[System.Web.Services.WebMethod]
	public System.Int32? Survey_Create(System.String name, System.Int32 type, System.Int32 isTemplate, System.Int32 templateID, System.Int32 reviewType)
	{
		try
		{
			return EFS.RC.CC.CH.Tables.Surveys.Create(name, (EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eType)type, isTemplate, templateID, (EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType)reviewType);
		}
		catch (System.Exception ex)
		{
			EFS.EventLog.Log.LogException(ex, null);
			throw ex;
		}
	}

	//get a given survey
	[System.Web.Services.WebMethod]
	public System.Xml.XmlDocument Survey_Get(System.Int32 id)
	{
		try
		{
			EFS.RC.CC.CH.Entities.Surveys.SurveyInfo surveyMsg = EFS.RC.CC.CH.Tables.Surveys.GetByID(id);
			return ToXmlDocument(surveyMsg);
		}
		catch (System.Exception ex)
		{
			EFS.EventLog.Log.LogException(ex, null);
			throw ex;
		}
	}

	//delete survey
	[System.Web.Services.WebMethod]
	public void Survey_Delete(System.Int32 id)
	{
		try
		{
			EFS.RC.CC.CH.Tables.Surveys.Delete(id);
		}
		catch (System.Exception ex)
		{
			EFS.EventLog.Log.LogException(ex, null);
			throw ex;
		}
	}
	#endregion

	#region SurveyCategory

	//get all surveyCategorys
	[System.Web.Services.WebMethod]
	public System.Xml.XmlDocument SurveyCategory_GetBySurveyID(System.Int32 surveyId)
	{
		try
		{
			EFS.RC.CC.CH.Entities.Surveys.Category[] surveyCategoryMsg = EFS.RC.CC.CH.Tables.SurveyCategories.GetBySurveyID(surveyId);
			return ToXmlDocument(surveyCategoryMsg);
		}
		catch (System.Exception ex)
		{
			// log
			EFS.EventLog.Log.LogException(ex, null);

			// throw
			throw ex;
		}
	}

	//update surveyCategory
	[System.Web.Services.WebMethod]
	public void SurveyCategory_Update(System.Int32 id, System.Int32 surveyId, System.String name, System.Int32 priority)
	{
		try
		{
			EFS.RC.CC.CH.Tables.SurveyCategories.Update(id, surveyId, name, priority);
		}
		catch (System.Exception ex)
		{
			// log
			EFS.EventLog.Log.LogException(ex, null);

			// throw
			throw ex;
		}
	}

	//create surveyCategory
	[System.Web.Services.WebMethod]
	public System.Int32? SurveyCategory_Create(System.Int32 surveyId, System.String name, System.Int32 priority)
	{
		try
		{
			return EFS.RC.CC.CH.Tables.SurveyCategories.Create(surveyId, name, priority);
		}
		catch (System.Exception ex)
		{
			// log
			EFS.EventLog.Log.LogException(ex, null);

			// throw
			throw ex;
		}
	}

	//get a given surveyCategory
	[System.Web.Services.WebMethod]
	public System.Xml.XmlDocument SurveyCategory_Get(System.Int32 id)
	{
		try
		{
			EFS.RC.CC.CH.Entities.Surveys.Category surveyCategoryMsg = EFS.RC.CC.CH.Tables.SurveyCategories.GetByID(id);
			return ToXmlDocument(surveyCategoryMsg);
		}
		catch (System.Exception ex)
		{
			// log
			EFS.EventLog.Log.LogException(ex, null);

			throw ex;
		}
	}

	//delete surveyCategory
	[System.Web.Services.WebMethod]
	public void SurveyCategory_Delete(System.Int32 id)
	{
		try
		{
			EFS.RC.CC.CH.Tables.SurveyCategories.Delete(id);
		}
		catch (System.Exception ex)
		{
			// log
			EFS.EventLog.Log.LogException(ex, null);

			// throw
			throw ex;
		}
	}
	#endregion

	#region SurveyQuestion

	//get all surveyQuestions
	[System.Web.Services.WebMethod]
	public System.Xml.XmlDocument SurveyQuestion_GetByCategoryID(System.Int32 categoryId)
	{
		try
		{
			EFS.RC.CC.CH.Entities.Surveys.Question[] surveyQuestionMsg = EFS.RC.CC.CH.Tables.SurveyQuestions.GetByCategoryId(categoryId);
			return ToXmlDocument(surveyQuestionMsg);
		}
		catch (System.Exception ex)
		{
			// log
			EFS.EventLog.Log.LogException(ex, null);

			// throw
			throw ex;
		}
	}

	//update surveyQuestion
	[System.Web.Services.WebMethod]
	public void SurveyQuestion_Update(
		System.Int32 id,
		System.Int32 categoryId,
		System.String text,
		System.Int32 priority,
		System.Int32 answersflowdirection)
	{
		try
		{
			EFS.RC.CC.CH.Tables.SurveyQuestions.Update(
				id,
				categoryId,
				text,
				priority,
				(EFS.RC.CC.CH.Entities.Surveys.Question.eAnswersFlowDirection)answersflowdirection);
		}
		catch (System.Exception ex)
		{
			// log
			EFS.EventLog.Log.LogException(ex, null);

			// throw
			throw ex;
		}
	}

	//create surveyQuestion
	[System.Web.Services.WebMethod]
	public System.Int32? SurveyQuestion_Create(
			System.Int32 categoryId,
			System.String text,
			System.Int32 priority,
			System.Int32 answersflowdirection)
	{
		try
		{
			return
				EFS.RC.CC.CH.Tables.SurveyQuestions.Create(
					categoryId,
					text,
					priority,
					(EFS.RC.CC.CH.Entities.Surveys.Question.eAnswersFlowDirection)answersflowdirection);
		}
		catch (System.Exception ex)
		{
			// log
			EFS.EventLog.Log.LogException(ex, null);

			// throw
			throw ex;
		}
	}

	//get a given surveyQuestion
	[System.Web.Services.WebMethod]
	public System.Xml.XmlDocument SurveyQuestion_Get(System.Int32 id)
	{
		try
		{
			EFS.RC.CC.CH.Entities.Surveys.Question surveyQuestionMsg = EFS.RC.CC.CH.Tables.SurveyQuestions.GetByID(id);
			return ToXmlDocument(surveyQuestionMsg);
		}
		catch (System.Exception ex)
		{
			// log
			EFS.EventLog.Log.LogException(ex, null);

			// throw
			throw ex;
		}
	}

	//delete surveyQuestion
	[System.Web.Services.WebMethod]
	public void SurveyQuestion_Delete(System.Int32 id)
	{
		try
		{
			EFS.RC.CC.CH.Tables.SurveyQuestions.Delete(id);
		}
		catch (System.Exception ex)
		{
			// log
			EFS.EventLog.Log.LogException(ex, null);

			// throw
			throw ex;
		}
	}
	#endregion

	#region SurveyAnswer

	//get all surveyAnswers
	[System.Web.Services.WebMethod]
	public System.Xml.XmlDocument SurveyAnswer_GetByQuesionID(System.Int32 quesionId)
	{
		try
		{
			EFS.RC.CC.CH.Entities.Surveys.Answer[] surveyAnswerMsg = EFS.RC.CC.CH.Tables.SurveyAnswers.GetByQuestionID(quesionId);
			return ToXmlDocument(surveyAnswerMsg);
		}
		catch (System.Exception ex)
		{
			// log
			EFS.EventLog.Log.LogException(ex, null);

			// throw
			throw ex;
		}
	}

	//update surveyAnswer
	[System.Web.Services.WebMethod]
	public void SurveyAnswer_Update(System.Int32 id, System.Int32 questionId, System.String name, System.String score, System.Int32 priority)
	{
		try
		{
			EFS.RC.CC.CH.Tables.SurveyAnswers.Update(id, questionId, name, score, priority);
		}
		catch (System.Exception ex)
		{
			// log
			EFS.EventLog.Log.LogException(ex, null);

			// throw
			throw ex;
		}
	}

	//create surveyAnswer
	[System.Web.Services.WebMethod]
	public System.Int32? SurveyAnswer_Create(System.Int32 questionId, System.String name, System.String score, System.Int32 priority)
	{
		try
		{
			return EFS.RC.CC.CH.Tables.SurveyAnswers.Create(questionId, name, score, priority);
		}
		catch (System.Exception ex)
		{
			// log
			EFS.EventLog.Log.LogException(ex, null);

			// throw
			throw ex;
		}
	}

	//get a given surveyAnswer
	[System.Web.Services.WebMethod]
	public System.Xml.XmlDocument SurveyAnswer_Get(System.Int32 id)
	{
		try
		{
			EFS.RC.CC.CH.Entities.Surveys.Answer surveyAnswerMsg = EFS.RC.CC.CH.Tables.SurveyAnswers.GetByID(id);
			return ToXmlDocument(surveyAnswerMsg);
		}
		catch (System.Exception ex)
		{
			EFS.EventLog.Log.LogException(ex, null);
			throw ex;
		}
	}

	//delete surveyAnswer
	[System.Web.Services.WebMethod]
	public void SurveyAnswer_Delete(System.Int32 id)
	{
		try
		{
			EFS.RC.CC.CH.Tables.SurveyAnswers.Delete(id);
		}
		catch (System.Exception ex)
		{
			EFS.EventLog.Log.LogException(ex, null);
			throw ex;
		}
	}
	#endregion

	#endregion

	#region Classifications
	//get a given Classification
	[System.Web.Services.WebMethod]
	public System.Xml.XmlDocument Classifications_GetDetails(System.Int32 id)
	{
		try
		{
			EFS.RC.CC.CH.Entities.Classification classificationMsg = EFS.RC.CC.CH.Tables.Classifications.GetByID(id);
			return ToXmlDocument(classificationMsg);
		}
		catch (System.Exception ex)
		{
			EFS.EventLog.Log.LogException(ex, null);
			throw ex;
		}
	}

	//get all classifications
	[System.Web.Services.WebMethod]
	public System.Xml.XmlDocument Classifications_GetByLineId(System.Int32 lineId)
	{
		try
		{
			EFS.RC.CC.CH.Entities.Classification[] classificationMsg = EFS.RC.CC.CH.Tables.Classifications.GetByLineId(lineId);
			return ToXmlDocument(classificationMsg);
		}
		catch (System.Exception ex)
		{
			EFS.EventLog.Log.LogException(ex, null);
			throw ex;
		}
	}

	//get all classifications
	[System.Web.Services.WebMethod]
	public System.Xml.XmlDocument Classifications_GetAll()
	{
		try
		{
			EFS.RC.CC.CH.Entities.Classification[] classificationMsg = EFS.RC.CC.CH.Tables.Classifications.GetAll();
			return ToXmlDocument(classificationMsg);
		}
		catch (System.Exception ex)
		{
			EFS.EventLog.Log.LogException(ex, null);
			throw ex;
		}
	}

	//update classification
	[System.Web.Services.WebMethod]
	public void Classification_Update(System.Int32 id, System.String name, System.Int32? customerEndpointID)
	{
		try
		{
			EFS.RC.CC.CH.Tables.Classifications.Update(id, name, customerEndpointID);
		}
		catch (System.Exception ex)
		{
			EFS.EventLog.Log.LogException(ex, null);
			throw ex;
		}
	}

	//create classification
	[System.Web.Services.WebMethod]
	public System.Int32? Classification_Create(System.String name, System.Int32? customerEndpointID)
	{
		try
		{
			return EFS.RC.CC.CH.Tables.Classifications.Create(name, customerEndpointID);
		}
		catch (System.Exception ex)
		{
			EFS.EventLog.Log.LogException(ex, null);
			throw ex;
		}
	}

	//delete classification
	[System.Web.Services.WebMethod]
	public void Classification_Delete(System.Int32 id)
	{
		try
		{
			EFS.RC.CC.CH.Tables.Classifications.Delete(id);
		}
		catch (System.Exception ex)
		{
			EFS.EventLog.Log.LogException(ex, null);
			throw ex;
		}
	}
	#endregion

	#region HuntGroups
	[System.Web.Services.WebMethod]
	public System.Xml.XmlDocument HuntGroups_GetDetails(System.Int32 id)
	{
		try
		{
			EFS.RC.CC.CH.Entities.HuntGroup huntgroupMsg = EFS.RC.CC.CH.Tables.HuntGroups.GetByID(id);
			return ToXmlDocument(huntgroupMsg);
		}
		catch (System.Exception ex)
		{
			EFS.EventLog.Log.LogException(ex, null);
			throw ex;
		}
	}

	//get all huntgroups
	[System.Web.Services.WebMethod]
	public System.Xml.XmlDocument HuntGroups_GetAll()
	{
		try
		{
			EFS.RC.CC.CH.Entities.HuntGroup [] huntgroupMsg = EFS.RC.CC.CH.Tables.HuntGroups.GetByAll();
			return ToXmlDocument(huntgroupMsg);
		}
		catch (System.Exception ex)
		{
			EFS.EventLog.Log.LogException(ex, null);
			throw ex;
		}
	}

	//update huntgroup
	[System.Web.Services.WebMethod]
	public void HuntGroup_Update(System.Int32 id, System.String name,System.String sip)
	{
		try
		{
			EFS.RC.CC.CH.Tables.HuntGroups.Update(id, name,sip);
		}
		catch (System.Exception ex)
		{
			EFS.EventLog.Log.LogException(ex, null);
			throw ex;
		}
	}

	//create huntgroup
	[System.Web.Services.WebMethod]
	public System.Int32? HuntGroup_Create(System.String name,System.String sip)
	{
		try
		{
			return EFS.RC.CC.CH.Tables.HuntGroups.Create(name,sip);
		}
		catch (System.Exception ex)
		{
			EFS.EventLog.Log.LogException(ex, null);
			throw ex;
		}
	}

	//delete huntgroup
	[System.Web.Services.WebMethod]
	public void HuntGroup_Delete(System.Int32 id)
	{
		try
		{
			EFS.RC.CC.CH.Tables.HuntGroups.Delete(id);
		}
		catch (System.Exception ex)
		{
			EFS.EventLog.Log.LogException(ex, null);
			throw ex;
		}
	}

	#endregion

    #region Test

    //get all skills
    [System.Web.Services.WebMethod]
    public System.String CheckExceptionsLog()
    {
        try
        {
            // make path
            System.String sectionPath = "EFS/EventLog/File/Config";

            // read section 
            EFS.Common.Config.Reader conf = EFS.Common.Config.Reader.TryCreateReader(sectionPath, false);

            if (!EFS.EventLog.Config.Log.FileLog.Filter.Except)
            {
                return "Exception Log Disabled";
            }

            EFS.AD.Domain sd = global::Domain.Instance;

            throw new System.Exception("Test Exception");
        }
        catch (System.Exception ex)
        {
            try
            {
                EFS.EventLog.Log.LogException(ex, null);
            }
            catch (System.Exception ex1)
            {
                return ex1.Message;
            }
        }

        // ok
        return "Ok";
    }

    #endregion
}
